package com.csizg.facerecognition;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.csizg.facemodel.util.CompareResult;
import com.csizg.facemodel.util.FaceUtil;
import com.csizg.facemodel.view.ICompareListener;
import com.csizg.facemodel.view.IFaceShowListener;
import com.csizg.facemodel.view.MyFaceCameraView;
import com.csizg.facemodel.view.RectBitmap;
import org.opencv.android.CameraBridgeViewBase;

/**
 * @author haohao(ronghao3508@gmail.com) on 2018/5/25 16:43
 * @version v1.0
 */
public class MyDialog extends Dialog {

    private String[] mDetectorName;

    private MyFaceCameraView mOpenCvCameraView;
    private RectBitmap mImageView;
    private ImageView mImageView1;
    private ImageView mImageView2;

    private TextView mSwitchTextView;
    private TextView mSaveTextView;
    private TextView mCompareTextView;

    View view;

    public MyDialog(@NonNull Context context) {
        super(context);
        view = View.inflate(getContext(), R.layout.dialog_layout, null);
        setContentView(view);
        init();
    }

    public MyDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        view = View.inflate(getContext(), R.layout.dialog_layout, null);
        setContentView(view);
        init();
    }

    protected MyDialog(@NonNull Context context, boolean cancelable,
            @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        view = View.inflate(getContext(), R.layout.dialog_layout, null);
        setContentView(view);
        init();
    }

    public void init() {
        mOpenCvCameraView = view.findViewById(R.id.fd1_activity_surface_view);
        mOpenCvCameraView.setVisibility(CameraBridgeViewBase.VISIBLE);
        mOpenCvCameraView.setCameraIndex(CameraBridgeViewBase.CAMERA_ID_FRONT);
        mOpenCvCameraView.setWindowManager(getWindow().getWindowManager());

        mImageView = view.findViewById(R.id.fd1_image_view);
        mImageView.setScaleType(ImageView.ScaleType.FIT_XY);

        mImageView1 = view.findViewById(R.id.fd1_switch_image1);
        mImageView2 = view.findViewById(R.id.fd1_switch_image2);

        mSwitchTextView = view.findViewById(R.id.fd1_switch);
        mSwitchTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOpenCvCameraView.switchCamera();
            }
        });

        mSaveTextView = view.findViewById(R.id.fd1_save);
        mSaveTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOpenCvCameraView.setCaptureFace();
            }
        });

        mCompareTextView = view.findViewById(R.id.fd1_compare);
        mCompareTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCompareTextView.setText("识别中，请勿重复点击");
                mImageView2.setImageBitmap(null);

                mOpenCvCameraView.compareFace();
            }
        });

        mOpenCvCameraView.setICompareListener(new ICompareListener() {
            @Override
            public void onCompare(CompareResult result) {
                if (result == null) {
                    mCompareTextView.setText("无法识别");
                } else {
                    mCompareTextView.setText("该人为" + result.getLabel() + "");
                    if (result.getLabel() == 1d) {
                        Bitmap bitmap = result.getBitmap();
                        if (bitmap == null) {
                            bitmap = FaceUtil.fileToImage(result.getPath());
                            if (bitmap != null) {
                                mImageView2.setImageBitmap(bitmap);
                            }
                        } else {
                            mImageView2.setImageBitmap(bitmap);
                        }
                    }
                }
            }

            @Override
            public void onCapture(boolean isTrue) {
                if (isTrue) {
                    Bitmap bitmap = FaceUtil.fileToImage(mOpenCvCameraView.getCurrentPath());
                    if (bitmap != null) {
                        mImageView1.setImageBitmap(bitmap);
                    }
                }
                Toast.makeText(getContext(), isTrue ? "存储图片成功" : "存储图片失败", Toast.LENGTH_SHORT)
                        .show();
            }
        });

        mOpenCvCameraView.setIFaceShowListener(new IFaceShowListener() {
            @Override
            public void onShow(Bitmap result) {
                mImageView.updateRect(result);
            }
        });

        mOpenCvCameraView.setIsShowResult(true);
    }

    @Override
    public void show() {
        super.show();
        System.loadLibrary("opencv_java3");
        System.loadLibrary("detection_based_tracker_jni");

        mOpenCvCameraView.enableView();
        mOpenCvCameraView.enableFpsMeter();
    }
}
